package com.example.demo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Arrays;
@RequestMapping("/session")
@RestController
public class Session {
    @GetMapping("/getC")
    public String getcookie(HttpServletRequest request){
        //获取所有cookie消息
    Cookie[] cookies= request.getCookies();
    //打印cookie
        if(cookies!=null){
            Arrays.stream(cookies).forEach(ck -> System.out.println(ck.getName()+":"+ck.getValue()));
        }
        return "获取cookie";
    }
    @RequestMapping("/getC2")
    public String getCookie2(@CookieValue("name") String name){
        return "从Cookie中获取值, name:"+name;
    }
    @RequestMapping("/setSess")
    public String setSess(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("name","zhang");
        return "设置成功";
    }
    @RequestMapping("/getS")
    public String getS(@SessionAttribute("name") String name){
        return "获取session";
    }
}
